#ifndef _SOUND_H
#define _SOUND_H

	public static Player sound;
	public static boolean sound_enabled = true;

	#ifdef SOUND_SCREEN_DEBUG
	public static boolean errorPlaying = false;
	public static String errorPlayingStr = "";
	public static boolean errorStopping = false;
	public static String errorStoppingStr = "";
	#endif
		
	public void playSound(int index, int loop, boolean forcePlay)
	{
		if(!sound_enabled)
			return;
		
		if(index > SOUND.SOUND_NAME.length)
		{
			System.out.println("INVALID SOUND INDEX " + index + ". Highest index is " + SOUND.SOUND_NAME.length);
			return;
		}
		
		try
		{
			if(sound != null && forcePlay)
			{
					if(sound.getState() == Player.STARTED)
					{
						sound.stop();
					}
					#ifdef USE_ELBA_SOUND
					sound.deallocate();
					sound.close();
					sound = null;
					#endif
			}
			
			if(sound == null || forcePlay)
			{
				InputStream is = getClass().getResourceAsStream(SOUND.SOUND_NAME[index]);
				sound = Manager.createPlayer(is,"audio/midi");
				sound.realize();
				sound.prefetch();
				#ifndef DONT_SET_LOOP_COUNT
				sound.setLoopCount(loop);
				#endif
				sound.start();
			}
		}
		catch(Exception e)
		{
			System.out.println("ERROR: " + e.toString());
			e.printStackTrace();
			#ifdef SOUND_SCREEN_DEBUG
			errorPlaying = true;
			errorPlayingStr = "ERROR: " + e.toString();
			#endif
		}
	}
	
	public void stopSound()
	{
		try
		{
			if(sound != null && sound.getState() == Player.STARTED)
			{
				sound.stop();
			}
			
			if(sound != null)
			{
				#ifdef USE_ELBA_SOUND
				sound.deallocate();
				sound.close();
				#endif
				sound = null;
			}
		}
		catch(Exception e)
		{
			System.out.println("ERROR: " + e.toString());
			e.printStackTrace();
			#ifdef SCREEN_DEBUG
			errorStopping = true;
			errorStoppingStr = "ERROR: " + e.toString();
			#endif
		}
	}
	
#endif